package com.aimashi.railwaytoolmanagesystem.activity.history;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.aimashi.railwaytoolmanagesystem.R;
import com.aimashi.railwaytoolmanagesystem.bean.HistoryInfo;
import com.aimashi.railwaytoolmanagesystem.bean.HistorySecondInfo;
import com.aimashi.railwaytoolmanagesystem.bean.ScanMode;

public class RecordAdapter extends BaseExpandableListAdapter {
	List<HistoryInfo> historyInfo;
	private LayoutInflater inflater;
	private Context context;

	public RecordAdapter(Context context, List<HistoryInfo> pHistoryInfo) {
		this.historyInfo = pHistoryInfo;
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	// group method stub
	public HistoryInfo getGroup(int groupPosition) {
		return historyInfo.get(groupPosition);
	}

	public int getGroupCount() {
		return historyInfo.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public HistorySecondInfo getChild(int groupPosition, int childPosition) {
		return historyInfo.get(groupPosition).getHistorySecondInfoList().get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public int getChildrenCount(int groupPosition) {
		return historyInfo.get(groupPosition).getHistorySecondInfoList().size();
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		HistoryInfo map = getGroup(groupPosition);
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = inflater.inflate(R.layout.hostory_record_item, null);

			viewHolder.recordDateText = (TextView) convertView.findViewById(R.id.recordDateText);
			viewHolder.scanListView = (ListView) convertView.findViewById(R.id.scanListView);
			viewHolder.groupImage = (ImageView) convertView.findViewById(R.id.groupImageView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.recordDateText.setText(map.getRecordMode().getRecordDate());

		// ����չ��ʱ��ͼ��
		if (isExpanded)
			viewHolder.groupImage.setImageResource(R.drawable.open);
		else
			viewHolder.groupImage.setImageResource(R.drawable.close);
		// if (viewHolder.scanListView != null)
		// viewHolder.scanListView.setAdapter(new ScanAdapter(context, map
		// .ScanList()));

		// Utility.setListViewHeightBasedOnChildren(viewHolder.scanListView);
		return convertView;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isExpanded, View convertView, ViewGroup parent) {
		HistorySecondInfo historySecondInfo = getChild(groupPosition, childPosition);
		ScanMode map = historySecondInfo.getScanMode();
		ChildViewHolder viewHolder;

		viewHolder = new ChildViewHolder();
		convertView = inflater.inflate(R.layout.hostory_scan_item, null);

		viewHolder.scanDescText = (TextView) convertView.findViewById(R.id.scanDescText);
		viewHolder.scanImageView = (ImageView) convertView.findViewById(R.id.scanImageView);
		viewHolder.goodsListView = (ListView) convertView.findViewById(R.id.goodsListView);

		viewHolder.scanDescText.setText("第"+map.getScanCount()+"扫描      【" + map.getEndTime()+"】");

		String fileName = map.getImgPath();
		File f = new File(fileName);
		if (f.exists()) { /* 产生Bitmap对象，并放入mImageView中 */
			  BitmapFactory.Options options = new BitmapFactory.Options();
		        options.inJustDecodeBounds = true;
		        // 获取这个图片的宽和高
		        Bitmap bitmap = BitmapFactory.decodeFile(fileName, options);  //此时的bitmap为null
		        options.inJustDecodeBounds = false;
		         //计算缩放比
		        int be = (int)(options.outHeight / (float)200);
		        if (be <= 0)
		            be = 1;
		        options.inSampleSize = be;
		        //重新读入图片，注意这次要把options.inJustDecodeBounds 设为 false
		        bitmap=BitmapFactory.decodeFile(fileName,options);     //此时的bitmap不为null
		        
			//Bitmap bm = BitmapFactory.decodeFile(fileName);
			viewHolder.scanImageView.setImageBitmap(bitmap);
		}

		if (viewHolder.goodsListView != null)
			viewHolder.goodsListView.setAdapter(new GoodsAdapter(context, historySecondInfo.getGoodModeList()));

		Utility.setListViewHeightBasedOnChildren(viewHolder.goodsListView);

		return convertView;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public boolean hasStableIds() {
		return false;
	}

	public static class ViewHolder {
		public TextView recordDateText;
		public ListView scanListView;
		public ImageView groupImage;
	}

	public static class ChildViewHolder {
		public TextView scanDescText;
		public ImageView scanImageView;
		public ListView goodsListView;
		// public TextView mTextNumber;
		// public CheckBox mSelected;
	}

}
